/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include "wsn-platform.h"
#include "cc2420.h"
#include "ARM/arm-common.h"	
#include "rtos-wsn.h"

#ifdef _Debug_zigbee_
	
#include "zigbee.def"
	
	extern BYTE RxBuffer[RX_BUFFER_SIZE];
	extern BYTE RxRead;
	extern BYTE RxWrite;
#endif


void cc2420_io_init(void)
{
	unsigned save;

	/* GPC1, GPC2 output; GPC3, GPC4 input ;Enable Pull-up resistor*/
	rGPCCON &= ~(0xFF << 2);
	rGPCCON |= 0x14;
    rGPCUP 	&= ~(0x0F<<1);
    
    /* GPF1 is EINT1, GPF2 is input, Enable GF1 GF2 Pull-up resistor*/
	rGPFCON &= ~(0x0F << 2);
    rGPFCON |=  (2 << 2);	
    rGPFUP 	&= ~(3 << 1);		

	/* GPG4 output, GPG5 SPIMISO1 ,GPG6 SPIMOSI1 ,GPG7 SPICLK1,Enable Pull-up resistor*/
	rGPGCON &= ~(0xFF << 8);
    rGPGCON |= 0x0000FD00;	
    rGPGUP 	&= ~(0x0F << 4);

    /* Set interrupt control. EINT1 is falling edge triggered */
    save = rEXTINT0;
    rEXTINT0 = (save & 0xFFFFFF0F) | (2 << 4);

}



/* CC2420 FIFOP ISR */
void isr_cc2420_fifop(void)
{
	
	int count = 0;
	ClearPending(BIT_EINT1);
	wsn_LeaveCritical();	
#ifdef _Debug_zigbee_
	if (CC2420_RX_IS_OVERFLOW()) {
        	cc2420_flush_Rxfifo();
        	RxWrite = 0;
			return;
   	 }
  while (CC2420_FIFO_IS_ACTIVE()/* Data in FIFO */) {
		count = cc2420_read_fifo(&RxBuffer[RxWrite], RX_BUFFER_SIZE - RxWrite);
		RxWrite += count;
		if (RxWrite >= RX_BUFFER_SIZE) /* Data error */
	    	RxWrite = 0;
  }
#endif
}

void cc2420_enable_fifop(void)
{
	rINTMSK &= ~BIT_SET(FIFOP);
}

void cc2420_disable_fifop(void)
{
	rINTMSK |= BIT_SET(FIFOP);
}
